package com.atguigu.educms.controller;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.cron.vo.QuestionQueryVo;
import com.atguigu.common.jwt.JwtUtils;
import com.atguigu.educms.entity.QaAnswer;
import com.atguigu.educms.entity.QaQuestion;
import com.atguigu.educms.service.QaQuestionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 问答-帖子表 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-03-14
 */
@RestController
@RequestMapping("/educms/qa-question")
public class QaQuestionController {

    @Autowired
    private QaQuestionService questionService;

    @ApiOperation(value = "发布问题")
    @PostMapping("/publish")
    public R publish(@RequestBody QaQuestion question, @ApiIgnore HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        try {
            if (!StringUtils.isEmpty(question)) {
                questionService.save(question);
                return R.ok();
            } else {
                return R.error();
            }
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "帖子列表")
    @PostMapping("/getQuestionList/{pageNo}/{pageSize}")
    public R getQuestionList(@PathVariable("pageNo") Integer pageNo,
                             @PathVariable("pageSize") Integer pageSize,
                             @RequestBody(required = false) QuestionQueryVo questionQueryVo) {
        FrontPageBean frontPageBean = questionService.getQuestionList(pageNo, pageSize,questionQueryVo);
        return R.ok().data(frontPageBean);
    }
}
